package io.itit.ecp.admin.provide.action.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.MiniProgramContentQueryReq;
import io.itit.ecp.admin.provide.action.resp.MiniProgramContentResp;
import io.itit.ecp.admin.server.entity.MiniProgramContentEntity;
import io.itit.ecp.admin.server.service.IMiniProgramContentService;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IAdd;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.common.provide.action.validation.IUpdate;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 小程序首页管理
 */
@Api(tags = "小程序首页管理")
@RestController
@RequestMapping("/ecp-admin/miniProgram/content")
public class MiniProgramContentController {

    @Autowired
    private IMiniProgramContentService miniProgramContentService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:miniProgramContent:search")
    public ResultResp<IPage<MiniProgramContentResp>> queryPage(@RequestBody @Validated(IQueryPage.class) MiniProgramContentQueryReq req) {
        Page<MiniProgramContentResp> page = buildQW(req).joinPage(PageUtils.getPage(req, MiniProgramContentResp.class), MiniProgramContentResp.class);
        return ResultResp.success(page);
    }

    private static JoinLambdaWrapper<MiniProgramContentEntity> buildQW(MiniProgramContentQueryReq req) {
        return Joins.of(MiniProgramContentEntity.class)
                .like(StringUtils.hasLength(req.getName()), MiniProgramContentEntity::getName, req.getName())
                .orderByDesc(StringUtils.hasLength(req.getSortColumn()), MiniProgramContentEntity::getCreateTm);
    }

    @GetMapping("get")
    public ResultResp<MiniProgramContentResp> get(@RequestParam String id) {
        MiniProgramContentEntity miniProgram = miniProgramContentService.getById(id);
        return ResultResp.success(ConvertUtils.convert(miniProgram, MiniProgramContentResp.class));
    }

    @ApiOperation("新增")
    @PostMapping("add")
    @RequiresPermissions("ecp-admin:miniProgramContent:add")
    public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) MiniProgramContentEntity req) {
        return ResultResp.success(miniProgramContentService.save(req));
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @RequiresPermissions("ecp-admin:miniProgramContent:update")
    public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) MiniProgramContentEntity req) {
        return ResultResp.success(miniProgramContentService.updateById(req));
    }

    @ApiOperation("删除(根据Id删除)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:miniProgramContent:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(miniProgramContentService.removeById(req.getId()));
    }

    @ApiOperation("批量删除(根据Id批量删除)")
    @PostMapping("batchDelete")
    @RequiresPermissions("ecp-admin:miniProgramContent:batchDelete")
    public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
        return ResultResp.success(miniProgramContentService.removeBatchByIds(req.getIds()));
    }

}
